<?php
/**
 * supply image upload and resize function.
 *
 * @package SGN_Core
 * @version $Id: Upload.php 1437 2012-03-16 10:44:10Z nhudt $
 * @author $Author: nhudt $
 *
 */
class SGN_Scribe
{
	const INSTANCE = 'SGN';
	
	private $channel = '';
	private $host = '';
	private $port = null;
	
	static $_self = array();
	
	private function __construct($channel, $host, $port)
	{
	    $this->channel = $channel;
	    $this->host = $host;
	    $this->port = $port;
	}
	
	/**
	 * Create unique object for each $instance.
	 * 
	 * @param string $instance
	 * 
	 * @return SGN_Scribe
	 */
	public static function factory($instance = '')
	{
	    if(!$instance) {
	        $instance = SGN_Scribe::INSTANCE;
	    }
	    
	    if(isset(self::$_self[$instance])) {
	        return self::$_self[$instance];
	    }
	    
	    $config = SGN_Application::getConfig('sgn', 'log', 'scribe');
	    if(!$config->$instance){
	        throw new Exception('Invalid Scribe instance: ' . $instance);
	    }
	    if(!$config->$instance->channel || !$config->$instance->host || !$config->$instance->port){
	        throw new Exception('Scribe config error: instance ' . $instance . ' does not have enough components!');
	    }
	    self::$_self[$instance] = new self($config->$instance->channel, $config->$instance->host, $config->$instance->port);
	    return self::$_self[$instance];
	}
	
	public function write($content)
	{
	    if(!is_string($content)){
	        $content = serialize($content);
	    }
	    scribelog($content, $this->channel, $this->host, $this->port);
	}
	
	
}